﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Model.CSP;
using CSP.Bll;
using CSP.Model.CSPEnum;
using CSP.Common.Tools;

namespace CSP.Web.UserControls
{
    public partial class LeftCategory : System.Web.UI.UserControl
    {
        private string _jsonServiceType;

        private string _jsonServiceArea;

        private string _jsonAgeGroup;

        protected string JsonServiceType
        {
            get
            {
                return _jsonServiceType;
            }
        }

        protected string JsonServiceArea
        {
            get
            {
                return _jsonServiceArea;
            }
        }

        protected string JsonAgeGroup
        {
            get
            {
                return _jsonAgeGroup;
            }
        }
         /// <summary>
        /// 当程序未初始化控件时加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Page.Request["getcategoryinfo"] != null)
            {
                PageInit();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        private void PageInit()
        {
            List<ServiceType> serviceTypes = GetServiceTypes();
            List<ServiceArea> serviceAreas = GetServiceArea();
            List<AgeGroup> ageGroups = GetAgeGroups();

            _jsonServiceType = CommonTool.SerializeObject(GetCategoryDataByServiceTypes(serviceTypes));
            _jsonServiceArea = CommonTool.SerializeObject(GetCategoryDataByServiceAreas(serviceAreas));
            _jsonAgeGroup = CommonTool.SerializeObject(ageGroups);

            string result = "{\"ServiceType\":" + _jsonServiceType + ",\"ServiceArea\":" + _jsonServiceArea + ",\"AgeGroup\":" + _jsonAgeGroup + "}";
            Response.Clear();
            Response.Write(result);
            Response.End();
        }

        /// <summary>
        /// 根据服务类型获取分类数据
        /// </summary>
        /// <param name="serviceTypes"></param>
        /// <returns></returns>
        private List<CategoryData> GetCategoryDataByServiceTypes(List<ServiceType> serviceTypes)
        {
            List<CategoryData> result = new List<CategoryData>();
            for (int i = 0; i < serviceTypes.Count; i++)
            {
                result.Add(new CategoryData()
                {
                    Id = serviceTypes[i].Id,
                    ParentId = serviceTypes[i].ParentId,
                    Name = serviceTypes[i].Name,
                    Level = serviceTypes[i].Level,
                    OrderId = serviceTypes[i].OrderId,
                });
            }
            return result;
        }

        private List<CategoryData> GetCategoryDataByServiceAreas(List<ServiceArea> serviceAreas)
        {
            List<CategoryData> result = new List<CategoryData>();
            for (int i = 0; i < serviceAreas.Count; i++)
            {
                result.Add(new CategoryData()
                {
                    Id = serviceAreas[i].Id,
                    ParentId = serviceAreas[i].Parentid,
                    Name = serviceAreas[i].Name,
                    Level = serviceAreas[i].Level,
                    OrderId = serviceAreas[i].Orderid,
                });
            }
            return result;
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceType> GetServiceTypes()
        {
            List<CSP.Model.CSP.ServiceType> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceTypeBll())
            {
                result = serviceTypeBll.FindAll().OrderBy(s => s.OrderId).ToList();
            }
            return result;
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceArea> GetServiceArea()
        {
            List<CSP.Model.CSP.ServiceArea> result;
            using (var serviceAreaBll = CSPBllFactory.GetInstance().GetServiceAreaBll())
            {
                //北京
                result = serviceAreaBll.FindAll().Where(a => a.Level == 2 && a.Parentid == 383).OrderBy(s => s.Orderid).ToList();
            }
            return result;
        }

        /// <summary>
        /// 获取年龄组列表
        /// </summary>
        /// <returns></returns>
        private List<AgeGroup> GetAgeGroups()
        {
            List<AgeGroup> result = new List<AgeGroup>();
            AgeGroup groupOneToThree = new AgeGroup();
            groupOneToThree.Id = Convert.ToInt32(AgeGroupType.OneToThree);
            groupOneToThree.Name = "0-3岁";

            AgeGroup groupThreeToSix = new AgeGroup();
            groupThreeToSix.Id = Convert.ToInt32(AgeGroupType.ThreeToSix);
            groupThreeToSix.Name = "3-6岁";

            AgeGroup groupSixToNine = new AgeGroup();
            groupSixToNine.Id = Convert.ToInt32(AgeGroupType.SixToNine);
            groupSixToNine.Name = "6-9岁";

            AgeGroup groupNineToTwelve = new AgeGroup();
            groupNineToTwelve.Id = Convert.ToInt32(AgeGroupType.NineToTwelve);
            groupNineToTwelve.Name = "9-12岁";

            AgeGroup groupTwelveToFifteen = new AgeGroup();
            groupTwelveToFifteen.Id = Convert.ToInt32(AgeGroupType.TwelveToFifteen);
            groupTwelveToFifteen.Name = "12-15岁";

            AgeGroup groupOverFifteen = new AgeGroup();
            groupOverFifteen.Id = Convert.ToInt32(AgeGroupType.OverFifteen);
            groupOverFifteen.Name = "15岁以上";

            result.Add(groupOneToThree);
            result.Add(groupThreeToSix);
            result.Add(groupSixToNine);
            result.Add(groupNineToTwelve);
            result.Add(groupTwelveToFifteen);
            result.Add(groupOverFifteen);

            return result;
        }

        private class CategoryData
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public int OrderId { get; set; }

            public int ParentId { get; set; }

            public int Level { get; set; }
        }

        private class AgeGroup
        {
            public int Id
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }
        }
    }
}